/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 弹出对话框 支持comfirm dialog 
 * @author : Chen weihan <csq-3@163.com>
 * @since : 2014年08月05日 星期二 14时38分32秒
 * @filename : swift/mobile/js/componnet/dialog/zepto.dialog.js
 * @version : 
 * @description : 
 */

/**
 * opt = {
        type
        dom
        content
        success
        cancel
   }
 */


;(function(){

   var dialog = {

       init : function(opt) {

           $(window).bind('orientationchange',function(){
               winWidth = window.innerWidth;
               var px = -(tthis.info.index * winWidth);
               tthis.transformCSS(px,0);
           });
       },

       open : function(opt) {
            switch(opt.type) {

               case 'comfirm':
                   this.comfirmDialog(opt);
               break;

               case 'page' : 
                   this.pageDialog(opt);            
               break;

            }
       },
        
       positionCover : function(dom) {
             var height  = $(document).height(); 
             dom.css({height : height+'px'});
       },

       positionDialog : function(dom,opt) {
             var sTop  = $(window).scrollTop(),
                 winH = $(window).height(),
                 domH = dom.height(),
                 top = sTop + (winH - domH)/2;
             dom.css({top : top+'px'});
       },
 
       backgroundCover : function(type) {
           var html = "<div class='bodyCover'></div>";
               html = $(html);
               this.positionCover(html);           
               /*
               html.on('touchstart touchmove touchend',function(e){
                     e.stopPropagation(); 
                     e.preventDefault(); 
               });
               */
           if ($('body').find('.bodyCover').length < 1 && type) { 
               $('body').append(html);
           } else if (!type) {
               $('body').find('.bodyCover').remove();         
           }
       },
 
       comfirmDialog : function(opt) {
           var dom = opt.dom,
               content = opt.content,
	       html =  "<div class='comfirmDialog'>"+
                       "     <div class='bgOpacity'></div>"+
		       "     <div class='comfirmTitle'></div>"+ 
		       "     <div class='comfirmContent'>"+content+"</div>"+ 
		       "     <div class='comfirmSubmit'>"+
		       "          <span class='comfirm'>YES</span> <span class='cancel'>NO</span>"+
		       "     </div>"+ 
		       "</div>";
               html = $(html);

               html.find('.comfirm').bind('tap',function(e){
                       html.remove(); 
                       opt.success == undefined ? '' : opt.success(e);               
               });

               html.find('.cancel').bind('tap',function(){
                       html.remove();
                       opt.cancel == undefined ? '' : opt.cancel(e);               
               });

           if (opt.cover) {
               this.backgroundCover(true);
           }

           if (dom.find('.comfirmDialog').length < 1) { 
                dom.append(html);
           }
       },

       pageDialog : function(opt) {
           var tthis = this,dom = opt.dom,
               content = opt.content,
	       html = "<div class='pageDialog'>"+
		      "     <div class='pageHeader'><span class='pageClose'> X </span></div>"+ 
		      "     <div class='pageContent'>"+content+"</div>"+ 
		      "</div>";

               html = $(html);
     
               html.on('touchstart touchmove touchend',function(e){
                      e.stopPropagation(); 
                      //e.preventDefault(); 
               });
 
               html.find('.pageClose').bind('touchend',function(e){
                      html.remove(); 
                      tthis.backgroundCover(false);
               });
 
           if (opt.cover) {
               this.backgroundCover(true);
           }

           if (dom.find('.pageDialog').length < 1) { 
               
               dom.append(html);
           }

           this.positionDialog(html,opt); 
       },



   }; 
   
   $.dialog = dialog;

   if (typeof define === "function" ) {
        define("dialog/zepto.dialog.js", [], function () { return $.dialog;} );
   }

})()

